import moment from 'moment'

//设置localstorage
export const setStorage = (key, val) => {
  let storage = window.localStorage
  if (storage) {
    storage.setItem(key, JSON.stringify(val))
  } else console.error('浏览器不支持LocalStorage,请更换浏览器！')
}

//获取localstorage
export const getStorage = (key) => {
  let storage = window.localStorage
  if (storage) {
    return JSON.parse(storage.getItem(key))
  } else console.error('浏览器不支持LocalStorage,请更换浏览器！')
}

export const timeFormSencend = (time, keyword) => {
  keyword = keyword || 'YYYY-MM-DD HH:mm:ss';
  if (time && time != '' && time != null) {
    return moment(time).format(keyword);
  } else return '-'
}

export const timeFormat = (time, keyword) => {
  keyword = keyword || 'YYYY-MM-DD';
    if (time && time != '' && time != null) {
      return moment(time).format(keyword);
    } else return '-'
}

export const bdEncrypt = (lng, lat) => {
  let X_PI = Math.PI * 3000.0 / 180.0;
  let x = lng, y = lat
  let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_PI)
  let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_PI)
  let bdLng = z * Math.cos(theta) + 0.0065
  let bdLat = z * Math.sin(theta) + 0.006
  return {
      latitude: bdLat,
      longitude: bdLng
  }
}
